package main

/*
conf.ini，键入内容：
; A comment line
[Section]
enabled = true
path = /usr/local # another comment
*/

import (
	"fmt"
	"log"

	"gopkg.in/gcfg.v1"
)

// ini文件的配置信息
type Server struct {
	Tcpport  string `json:"tcpport"`
	HttpPort string `json:"httpport"`
	Key      string `json:"key"`
}
type Database struct {
	Dbtype     string `json:"dbtype"`
	Dbip       string `json:"dbip"`
	Dbport     string `json:"dbport"`
	Dbuser     string `json:"dbuser"`
	Dbpassword string `json:"dbpassword"`
	Dbname     string `json:"dbname"`
}
type Config struct {
	Server
	Database
}

// 从ini文件读取数据库的配置信息
func ConfigInit() (cfg *Config, err error) {
	ini := Config{}
	err = gcfg.ReadFileInto(&ini, "config.ini")
	if err != nil {
		log.Printf("Failed to parse config file: %s \n", err)
		return cfg, err
	}

	cfg = &ini
	log.Println("config.ini:", cfg)
	return cfg, err
}

func main1() {

	c, err := ConfigInit()
	if err != nil {
		log.Println(err)
	}
	fmt.Println(c)
	fmt.Printf("httpport:%s,dbip:%s \n", c.Server.HttpPort, c.Database.Dbip)
	fmt.Printf("httpport:%s,dbip:%s \n", c.HttpPort, c.Dbip)
}
